package starter.kit.rx.app.feature.music;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import nucleus.factory.RequiresPresenter;
import starter.kit.feature.NetworkContract;
import starter.kit.feature.rx.RxStarterActivity;
import starter.kit.retrofit.ErrorResponse;
import starter.kit.retrofit.RetrofitException;
import starter.kit.rx.app.R;
import starter.kit.rx.app.model.bean.music.Musics;
import starter.kit.rx.app.util.DisplayImgUtis;
import starter.kit.rx.app.views.html5.HTML5WebViewActivity;
import work.wanghao.simplehud.SimpleHUD;

/**
 * ============================================================
 * 版权： xx 版权所有（c）2016
 * <p>
 * 作者：Loofer
 * 版本：1.0
 * 创建日期 ：2016/10/26 19:10.
 * 描述：
 * <p>
 * 注:如果您修改了本类请填写以下内容作为记录，如非本人操作劳烦通知，谢谢！！！
 * Modified Date Modify Content:
 * <p>
 * ==========================================================
 */
@RequiresPresenter(MusicDetailPresenter.class)
public class MusicDetailActivity extends RxStarterActivity<MusicDetailPresenter> implements NetworkContract.View {

    @BindView(R.id.iv_music)
    ImageView mIvMusic;
    @BindView(R.id.tv_music_name)
    TextView mTvMusicName;
    @BindView(R.id.tv_music_grade)
    TextView mTvMusicGrade;
    @BindView(R.id.tv_music_art)
    TextView mTvMusicArt;
    @BindView(R.id.tv_music_publishtime)
    TextView mTvMusicPublishtime;
    @BindView(R.id.tv_music_grade_num)
    TextView mTvMusicGradeNum;
    @BindView(R.id.tv_listen)
    TextView mTvListen;
    @BindView(R.id.tv_more_info)
    TextView mTvMoreInfo;
    @BindView(R.id.tv_description)
    TextView mTvDescription;
    @BindView(R.id.tv_songs)
    TextView mTvSongs;
    private String mId;//
    private Musics mMusics;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_detail);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        if (intent != null) {
            mId = intent.getStringExtra("id");
        }
        getPresenter().requestItem(mId);
    }

    private void initView() {
        DisplayImgUtis.getInstance().display(this, mMusics.getImage(), mIvMusic);
        mTvMusicName.setText(mMusics.getTitle());
        setTitle(mMusics.getTitle());
        if (mMusics.getAuthor() != null && mMusics.getAuthor().size() > 0) {
            mTvMusicArt.setText("艺术家：" + mMusics.getAuthor().get(0).getName());
        }
        if (mMusics.getAttrs() != null && mMusics.getAttrs().getPubdate() != null && mMusics.getAttrs().getPubdate().size() > 0) {
            mTvMusicPublishtime.setText(mMusics.getAttrs().getPubdate().get(0));
        }
        if (mMusics.getRating() != null) {
            if (!TextUtils.isEmpty(mMusics.getRating().getAverage()))
                mTvMusicGrade.setText(mMusics.getRating().getAverage());
            if (!TextUtils.isEmpty("" + mMusics.getRating().getNumRaters())) {
                mTvMusicGradeNum.setText(mMusics.getRating().getNumRaters() + "人评");
            }
        }
        if (!TextUtils.isEmpty(mMusics.getSummary())) {
            mTvDescription.setText(mMusics.getSummary());
        }
        if (mMusics.getAttrs().getTracks() != null && mMusics.getAttrs().getTracks().size() > 0) {
            mTvSongs.setText(mMusics.getAttrs().getTracks().get(0));
        }
    }

    @OnClick({R.id.tv_listen, R.id.tv_more_info})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_listen:
            case R.id.tv_more_info:
                Intent intent = new Intent(this, HTML5WebViewActivity.class);
                intent.putExtra(HTML5WebViewActivity.EXTRA_URL, mMusics.getAlt());
                startActivity(intent);
                break;
        }
    }


    @Override
    public void onSuccess(Object item) {
        mMusics = (Musics) item;
        initView();
    }

    @Override
    public void onError(RetrofitException exception) {
        try {
            ErrorResponse errorResponse = exception.getErrorBodyAs(ErrorResponse.class);
            SimpleHUD.showErrorMessage(this, errorResponse.getMessage());
        } catch (IOException e) {
            SimpleHUD.showErrorMessage(this, exception.getMessage());
        }
    }


}
